package com.atguigu.schedule.controlleer;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.schedule.POJO.SysUser;
import com.atguigu.schedule.common.Result;
import com.atguigu.schedule.common.ResultCodeEnum;
import com.atguigu.schedule.service.SysUserService;
import com.atguigu.schedule.service.impl.SysUserServiceImpl;
import com.atguigu.schedule.util.MD5Util;
import com.atguigu.schedule.util.WebUtil;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/user/*")
public class SysUserController extends BaseController{

    private SysUserService sysUserService = new SysUserServiceImpl();


    /**
     * 通过js异步校验用户名是否重复
     * @param req
     * @param res
     */
    protected void checkUsernameUsed(HttpServletRequest req,HttpServletResponse res) throws SQLException, IOException {
        String username = req.getParameter("username");

        //判断是否存在
        SysUser login = sysUserService.login(new SysUser(null, username, null));
        Result result=null;
        //如果不为空则重复
        if (login!=null){
            result=Result.build(null, ResultCodeEnum.USERNAME_USED);
        }else {
            result=Result.ok(null);
        }
        WebUtil.writeJson(res,result);

    }

    /**
     * 用户注册,校验用户名是否重复，如果未重复则注册成功。
     * @param req
     * @param res
     */
    protected void regist(HttpServletRequest req , HttpServletResponse res) {
       try {
           //获取参数
           String username = req.getParameter("username");
           String userPwd = req.getParameter("userPwd");
           //将参数封装成对象，并调用服务层方法
           SysUser sysUser = new SysUser(null, username, userPwd);
           int rows = sysUserService.regist(sysUser);

           //根据返回值判断是否注册成功，并做响应重定向
           if (rows == 0 ){
               res.sendRedirect("/registFail.html");
           } else {
               res.sendRedirect("/registSuccess.html");
           }
       }catch (Exception e){
           e.printStackTrace();
       }



    }

    /**
     * 用户登录业务处理
     * @param req
     * @param res
     * @throws IOException
     * @throws SQLException
     */
    protected void login(HttpServletRequest req , HttpServletResponse res) throws IOException, SQLException {
        //获取参数
        String username = req.getParameter("username");
        String userPwd = req.getParameter("userPwd");

        //判断用户是否登录成功
        SysUser sysUser = new SysUser(null, username, userPwd);
        SysUser user = sysUserService.login(sysUser);
        System.out.println(user);
        if (user == null){
            res.sendRedirect("/loginUsernameError.html");
        }else if (!MD5Util.encrypt(userPwd).equals(user.getUserPwd())){
            res.sendRedirect("/loginUserPwdError.html");
        }else {
            HttpSession session = req.getSession();
            session.setAttribute("SysUser",user);
            res.sendRedirect("/showDchedule.html");
        }
    }
}